package com.fang.algorithm.zuo.subsearch;

/**
 * @author lingxiao.fang
 * @Desc todo
 * @createTime 2025/2/6 15:23
 */
public class Test01_SubSearch {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int ansIndex = subSearch2(arr, 2);
        System.out.println(ansIndex);
    }

    private static int subSearch2(int[] arr, int target) {
        if (arr == null || arr.length < 1) {
            return -1;
        }
        int left = 0;
        int right = arr.length - 1;
        int targetIndex = -1;
        while (left <= right) {
            // 或者 left + ((right - left) >> 2)
            int mid = (left + right) / 2;
            if (arr[mid] == target) {
                targetIndex = mid;
                break;
            } else if (arr[mid] > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return targetIndex;
    }
}
